package com.ysxsoft.wdg.ui.activity.center_st;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.classic.common.MultipleStatusView;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.socks.library.KLog;
import com.ysxsoft.wdg.R;
import com.ysxsoft.wdg.adapter.center_st.StShopAdapter;
import com.ysxsoft.wdg.base.BaseActivity;
import com.ysxsoft.wdg.bean.BaseBean;
import com.ysxsoft.wdg.bean.st.CompanyBean;
import com.ysxsoft.wdg.net.CallbackCode;
import com.ysxsoft.wdg.util.ToastUtils;
import com.ysxsoft.wdg.util.sp.SharePrefUtils;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

public class ShopFjActivity extends BaseActivity implements OnRefreshListener, OnLoadmoreListener {


    public static void start(Context context) {
        Intent intent = new Intent(context, ShopFjActivity.class);
        context.startActivity(intent);
    }

    @BindView(R.id.iv0)
    ImageView iv0;
    @BindView(R.id.iv1)
    ImageView iv1;
    @BindView(R.id.iv2)
    ImageView iv2;
    @BindView(R.id.topView)
    View topView;
    @BindView(R.id.title_content)
    TextView titleContent;
    @BindView(R.id.title_iv_r)
    ImageView titleIvR;
    @BindView(R.id.tv0)
    TextView tv0;
    @BindView(R.id.tv1)
    TextView tv1;
    @BindView(R.id.tv2)
    TextView tv2;
    @BindView(R.id.recyclerview)
    RecyclerView recyclerview;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    @BindView(R.id.multStatusView)
    MultipleStatusView multStatusView;


    private TextView[] textViews;
    private ImageView[] imageViews;

    private int page = 1;
    private StShopAdapter mAdapter;
    private String address = "", lng = "", lat = "";

    private int xu = 1;


    @Override
    protected int getLayoutId() {
        return R.layout.activity_shop_fj;
    }

    @Override
    protected void initData() {
        initStatusBar(topView, false);
        titleContent.setText("附近商家");
        titleIvR.setImageResource(R.mipmap.zy_search);

        imageViews = new ImageView[]{iv0, iv1, iv2};
        textViews = new TextView[]{tv0, tv1, tv2};
        tv0.setSelected(true);

        //TODO 取消屏蔽
        address = SharePrefUtils.getAddress();
        lat = SharePrefUtils.getLatitude();
        lng = SharePrefUtils.getLongitude();


        recyclerview.setLayoutManager(new LinearLayoutManager(this));
        mAdapter = new StShopAdapter(R.layout.item_shop);
        recyclerview.setAdapter(mAdapter);


        multStatusView.showLoading();
        onRefresh(refreshLayout);

    }

    @Override
    protected void setListener() {
        refreshLayout.setOnLoadmoreListener(this);
        refreshLayout.setOnRefreshListener(this);

        mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                CompanyBean bean= (CompanyBean) adapter.getItem(position);
                StShopDetailActivity.start(ShopFjActivity.this,bean.getId());
            }
        });
    }


    @OnClick({R.id.title_finish, R.id.title_iv_r})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.title_finish:
                finish();
                break;
            case R.id.title_iv_r:
                StSearchActivity.start(mContext);
                break;
        }
    }

    //距离1正序2倒序  价格3正序4倒序    评分5倒序6正序
    @OnClick({R.id.tv0, R.id.tv1, R.id.tv2})
    public void onViewClicked2(View view) {
        switch (view.getId()) {
            case R.id.tv0:
                xu = (xu == 2) ? 1 : 2;
                upXu(0, xu == 1);
                break;
            case R.id.tv1:
                xu = (xu == 3) ? 4 : 3;
                upXu(1, xu == 4);
                break;
            case R.id.tv2:
                xu = (xu == 5) ? 6 : 5;
                upXu(2, xu == 5);
                break;
        }

        onRefresh(refreshLayout);
    }


    public void upXu(int position, boolean isUp) {
        for (int i = 0; i < imageViews.length; i++) {
            textViews[i].setSelected(i == position);
            if (i != position) {
                imageViews[i].setImageResource(R.mipmap.xu_default);
            } else {
                imageViews[i].setImageResource(isUp ? R.mipmap.xu_up : R.mipmap.xu_down);
            }
        }

    }


    @Override
    public void onLoadmore(RefreshLayout refreshlayout) {
        refreshlayout.finishLoadmore(500);

        page++;
        getList();
    }

    @Override
    public void onRefresh(RefreshLayout refreshlayout) {
        refreshlayout.finishRefresh(500);

        page = 1;
        mAdapter.clear();
        getList();
    }

    private void getList() {
        KLog.e("xu = " + xu);
        mApiHelper.getStFjList(page + "", address, lng, lat, xu + "", new Observer<BaseBean<List<CompanyBean>>>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(BaseBean<List<CompanyBean>> baseBean) {
                if (baseBean.getCode().equals(CallbackCode.SUCCESS)) {
                    multStatusView.showContent();
                    if (page == 1) {
                        mAdapter.setNewData(baseBean.getData());
                    } else {
                        mAdapter.addData(baseBean.getData());
                    }
                } else {
                    ToastUtils.showToast(baseBean.getMs());
                    if (page == 1) {
                        multStatusView.showEmpty();
                    }
                }
            }

            @Override
            public void onError(Throwable e) {
                multStatusView.showError();
            }

            @Override
            public void onComplete() {

            }
        });

    }

}
